package cn.stylefeng.guns.modular.project.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.project.entity.ProjectMemberWork;
import cn.stylefeng.guns.modular.project.mapper.ProjectMemberWorkMapper;
import cn.stylefeng.guns.modular.project.model.params.ProjectMemberWorkParam;
import cn.stylefeng.guns.modular.project.model.result.ProjectMemberWorkResult;
import cn.stylefeng.guns.modular.project.service.ProjectMemberWorkService;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2019-10-21
 */
@Service
public class ProjectMemberWorkServiceImpl extends ServiceImpl<ProjectMemberWorkMapper, ProjectMemberWork> implements ProjectMemberWorkService {

    @Override
    public void add(ProjectMemberWorkParam param) {
        ProjectMemberWork entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ProjectMemberWorkParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(ProjectMemberWorkParam param) {
        ProjectMemberWork oldEntity = getOldEntity(param);
        ProjectMemberWork newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ProjectMemberWorkResult findBySpec(ProjectMemberWorkParam param) {
        return null;
    }

    @Override
    public List<ProjectMemberWorkResult> findListBySpec(ProjectMemberWorkParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ProjectMemberWorkParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public int sumWorkHour(int projectId) {
        Integer workHour = this.baseMapper.sumWorkHour(projectId);
        return workHour == null ? 0 : workHour;
    }

    @Override
    public int sumWorkHour1(int projectId, long userId) {
        Integer workHour = this.baseMapper.sumWorkHour1(projectId, userId);
        return workHour == null ? 0 : workHour;
    }

    @Override
    @Transactional(rollbackOn = Exception.class)
    public void addWorkHourSelf(ProjectMemberWorkParam param) {
        ProjectMemberWork projectMemberWork = null;
        if (param.getProjectA() != null) {
            projectMemberWork = new ProjectMemberWork();
            projectMemberWork.setProjectId(param.getProjectA());
            projectMemberWork.setUserId(ShiroKit.getUser().getId());
            projectMemberWork.setWorkDate(param.getWorkDate());
            projectMemberWork.setWorkHour(param.getWorkHourA());
            projectMemberWork.setOvertimeHour(param.getOvertimeHourA());
            this.save(projectMemberWork);
        }
        if (param.getProjectB() != null) {
            projectMemberWork = new ProjectMemberWork();
            projectMemberWork.setProjectId(param.getProjectB());
            projectMemberWork.setUserId(ShiroKit.getUser().getId());
            projectMemberWork.setWorkDate(param.getWorkDate());
            projectMemberWork.setWorkHour(param.getWorkHourB());
            projectMemberWork.setOvertimeHour(param.getOvertimeHourB());
            this.save(projectMemberWork);
        }
        if (param.getProjectC() != null) {
            projectMemberWork = new ProjectMemberWork();
            projectMemberWork.setProjectId(param.getProjectC());
            projectMemberWork.setUserId(ShiroKit.getUser().getId());
            projectMemberWork.setWorkDate(param.getWorkDate());
            projectMemberWork.setWorkHour(param.getWorkHourC());
            projectMemberWork.setOvertimeHour(param.getOvertimeHourC());
            this.save(projectMemberWork);
        }
        if (param.getProjectD() != null) {
            projectMemberWork = new ProjectMemberWork();
            projectMemberWork.setProjectId(param.getProjectD());
            projectMemberWork.setUserId(ShiroKit.getUser().getId());
            projectMemberWork.setWorkDate(param.getWorkDate());
            projectMemberWork.setWorkHour(param.getWorkHourD());
            projectMemberWork.setOvertimeHour(param.getOvertimeHourD());
            this.save(projectMemberWork);
        }
        if (param.getProjectE() != null) {
            projectMemberWork = new ProjectMemberWork();
            projectMemberWork.setProjectId(param.getProjectE());
            projectMemberWork.setUserId(ShiroKit.getUser().getId());
            projectMemberWork.setWorkDate(param.getWorkDate());
            projectMemberWork.setWorkHour(param.getWorkHourE());
            projectMemberWork.setOvertimeHour(param.getOvertimeHourE());
            this.save(projectMemberWork);
        }
    }

    private Serializable getKey(ProjectMemberWorkParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ProjectMemberWork getOldEntity(ProjectMemberWorkParam param) {
        return this.getById(getKey(param));
    }

    private ProjectMemberWork getEntity(ProjectMemberWorkParam param) {
        ProjectMemberWork entity = new ProjectMemberWork();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
